<?php


namespace yuankezhan\yiiTableWidget;


use yii\helpers\Html;

class TableButton
{
    const TYPE_BUTTON = 'button';
    const TYPE_LINK = 'link';
    const TYPE_LINK_BLOCK = 'link_block';

    const THEME_DEFAULT = '';
    const THEME_WARM = 'public-table-btn-warm';
    const THEME_DANGER = 'public-table-btn-danger';
    const THEME_DISABLED = 'public-table-btn-disabled';

    /**
     * @param string $text 按钮文字
     * @param string $typeValue 按钮类型
     * @param string $type
     * @param string $theme THEME_DEFAULT  THEME_WARM  THEME_DANGER  THEME_DISABLED
     * @param string $permission
     * @param string $class
     * @return TableButton
     */
    public static function set(string $text, string $typeValue, string $type = self::TYPE_LINK, string $theme = self::THEME_DEFAULT, string $permission = "", string $class = " ")
    {
        return new static($text, $typeValue, $type, $theme, $permission, $class);
    }

    public string $text;
    public string $typeValue;
    public string $type;
    public string $theme;
    public string $permission;
    public string $class;

    public function __construct($text, $typeValue, $type, $theme, $permission, $class)
    {
        $this->text = $text;
        $this->typeValue = $typeValue;
        $this->type = $type;
        $this->theme = $theme;
        $this->permission = $permission;
        $this->class = $class;
    }

    public function create()
    {
        $btnClass = match ($this->theme) {
                TableButton::THEME_WARM => "public-table-btn-warm",
                TableButton::THEME_DANGER => "public-table-btn-danger",
                TableButton::THEME_DISABLED => "public-table-btn-disabled",
                default => "",
            } . $this->class;
        $btnStr = '';
        if ($this->type == TableButton::TYPE_LINK)
        {
            $btnStr .= Html::a($this->text, $this->typeValue, ['class' => "{$btnClass} public-table-btn-xs public-table-btn "]);
        }
        if ($this->type == TableButton::TYPE_LINK_BLOCK)
        {
            $btnStr .= Html::a($this->text, $this->typeValue, ['class' => "{$btnClass} public-table-btn-xs public-table-btn ", 'target' => '_blank']);
        }
        else if ($this->type == TableButton::TYPE_BUTTON)
        {
            $btnStr .= Html::button($this->text, ['class' => "{$btnClass} public-table-btn-xs public-table-btn ", 'onclick' => $this->typeValue]);
        }
        return $btnStr;
    }
}